package com.iborn.watchtoday.test;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.content.Intent;
import android.widget.ListView;  
import android.widget.SimpleCursorAdapter;  
import android.util.Log;

import java.util.Date;

import android.database.Cursor; 
import android.content.*;

import java.util.List;
import java.util.ArrayList;

import com.iborn.watchtoday.FilmConst;
import com.iborn.watchtoday.FilmInfo;
import com.iborn.watchtoday.FilmInfoAdapter;
import com.iborn.watchtoday.FilmServer;
import com.iborn.watchtoday.R;


public class TVActivity extends Activity {
	private Cursor cursor;  
	private FilmReceiver  receiver;
	private List<FilmInfo> filminfo;
	private FilmInfoAdapter filmadapter;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		Log.d(FilmConst.DEBUG_TAG_A,"start service");
		filminfo = new ArrayList<FilmInfo>();
		ListView lv = (ListView)findViewById(R.id.menuleft);
		filmadapter = new FilmInfoAdapter(this,filminfo,lv);
		lv.setAdapter(filmadapter);
		startService(new Intent(this,FilmServer.class));	
	}

	@Override
	public void onResume()
	{
		IntentFilter filter;
		filter = new IntentFilter(FilmConst.FILM_FINISH_LOAD);
		receiver = new FilmReceiver();
		registerReceiver(receiver,filter);
		super.onResume();
	}
	
	@Override
	public void onPause()
	{
		unregisterReceiver(receiver);
		super.onPause();
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.tv, menu);
		return true;
	}
	
	private void loadfilms()
	{
		long date = System.currentTimeMillis()-24*60*60*1000;
        cursor=getContentResolver().query(FilmConst.CONTENT_URI, null, FilmConst.KEY_FILMUPDATE + ">?",new String[]{Long.toString(date)}, null);  
        startManagingCursor(cursor);  
        if(cursor.moveToFirst())
        {
        	filminfo.clear();
        	do{
        		FilmInfo info = new FilmInfo();
        		info.filmId = cursor.getInt(FilmConst.FILMID_COLUMN);
        		info.filmEn = cursor.getString(FilmConst.FILMEN_COLUMN);
        		info.filmInfo = cursor.getString(FilmConst.FILMINFO_COLUMN);
        		info.filmLink = cursor.getString(FilmConst.FILMLINK_COLUMN);
        		info.filmName = cursor.getString(FilmConst.FILMNAME_COLUMN);
        		info.filmOva = cursor.getInt(FilmConst.FILMOVA_COLUMN);
        		info.filmPicture = cursor.getString(FilmConst.FILMPICTURE_COLUMN);
        		info.filmTag = cursor.getString(FilmConst.FILMTAG_COLUMN);
        		info.filmUpdate =new Date(cursor.getLong(FilmConst.FILMUPDATE_COLUMN));
        		info.addActor(cursor.getString(FilmConst.FILMACTORS_COLUMN));
        		filminfo.add(info);
        		filmadapter.notifyDataSetChanged();
        	}while(cursor.moveToNext());
         
        }
	}
	
	public class FilmReceiver extends BroadcastReceiver{
		@Override
		public void onReceive(Context context,Intent intent)
		{
			loadfilms();
		}
	}
}
